<?php

namespace App\Http\Controllers\Site;

use App\B2BProject\Custom\Business\GoodsCategory;
use App\B2BProject\Custom\Member;
use App\B2BProject\Custom\Provider\Offer;
use App\B2BProject\Site\HelperControllerBuildTrait;
use App\B2BProject\Area;
use App\B2BProject\Custom\Business\Store;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MerchantController extends Controller
{
    use HelperControllerBuildTrait;

    /**
     * 商家中心显示
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function getIndex(Request $request)
    {
        $cid = '';
        $pid = 0;
        $request->query->set('page', $request->query('page', $request->page));
        $request->query->set('pageSize', $request->query('perpage', $request->pageSize));

        if ($request->ajax()) {

            $query = Store::has('goods', '>', 5)->orderBy('created_at', 'desc')->where('audit_status',
                true)->where('store_logo', '!=', '');

            if ($brand_id = $request->query('brand_id')) {
                $query = $query->whereHas('mainBrand', function (Builder $query) use ($brand_id) {
                    $query->where('id', $brand_id);
                });
            }

            if ($goodType = $request->query('goodsType')) {
                $category = GoodsCategory::with('children')->whereId($goodType)->firstOrFail();
                if ($category->parent_id == 0) {
                    $in = [];
                    foreach ($category->children as $child) {
                        $in[] = $child->id;
                    }
                    $query = $query->whereHas('goods', function (Builder $query) use ($goodType) {
                        $query->where('class_id', $goodType);
                    });


                } else {
                    $query = $query->whereHas('goods', function (Builder $query) use ($goodType) {
                        $query->where('class_id', $goodType);
                    });

                }
            }

            if ($addr = $request->query('addr')) {
                $query = $query->whereHas('member.information', function (Builder $builder) use ($addr) {
                    $builder->where('province_id', $addr);
                });
            }

            if ($supplyer = $request->query('supplyer')) {
                $query = $query->whereHas('member', function (Builder $query) use ($supplyer) {
                    $query->whereHas('information', function (Builder $query) use ($supplyer) {
                        $query->where('company', 'like', "%$supplyer%");
                    });
                });
            }

            $stores = $query->paginate($request->query('pageSize', 7));
//TODO ???
            $data = [];

            foreach ($stores as $store) {

                $information = $store->member->information;
                $data[]      = [
                    'company'  => [
                        'img'      => $store->store_logo,
                        'href'     => route('store', [$store->id]),
                        'name'     => $information->company,
                        'addr'     => Area::getName($information->provonce_id) .
                            ' ' .
                            Area::getName($information->city_id),
                        'business' => call_user_func_array(function (Store $store) {
                            $categories = [];
                            foreach ($store->mainCategory as $category) {
                                $categories[] = $category->name;
                            }
                            return mb_substr(implode(' ', $categories), 0, 9) . '…';
                        }, [$store]),
                    ],
                    'products' => call_user_func_array(function (Store $store) {
                        $products = [];

                        foreach ($store->goods()->display()->orderBy('created_at', 'desc')->take(5)->get() as $good) {
                            $products[] = [
                                'img'  => $good->cover(),
                                'href' => route('goods.detail', [$good->id]),
                                'name' => $good->name,
                            ];
                        }
                        return $products;
                    }, [$store]),
                ];
            }

            return response()->json([
                'data'           => $data,
                'pagingConfData' => [
                    'page'     => $request->query('page'),
                    'pageSize' => $request->query('pageSize'),
                    'total'    => $stores->total(),
                ],
            ]);
        }
        return view('host/merchant')->with('cid', $cid)->with('pid', $pid);
    }


}
